package com.zhoushihao.class0812;

import java.util.*;

public class Poker {
//    public Color suits;
    public Card[] cards = new Card[52];

    public Poker() {
        int index = 0;
        for (Color suit : Color.values()) {
            for (int j = 2; j <= 14; j++) {
                cards[index++] = new Card(suit.getSuit(), j);
            }
        }
    }

    public void output() {
        int index = 0;
        for (Color suit : Color.values()) {
            for (int j = 1; j <= 13; j++) {
                System.out.print(cards[index++] + " ");
            }
            System.out.println();
        }
    }

    public List<Card> getOneHand() {
        List<Card> hand = new ArrayList<>();
//        this.shuffle();
        for (int i = 0; i < 5; i++) {
            hand.add(cards[i]);
        }
        return hand;
    }

    public static String getHandType(List<Card> hand) {
        Map<String, Integer> suitsMap = new HashMap<>();
        Map<Integer, Integer> cardsMap = new TreeMap<>();
        for (Card card : hand) {
            suitsMap.put(card.suit, suitsMap.getOrDefault(card.suit, 0) + 1);
            cardsMap.put(card.rank, cardsMap.getOrDefault(card.rank, 0) + 1);
        }
        if (cardsMap.size() == 2) {
            if (cardsMap.containsValue(4)) return "四条";
            return "满堂红";
        } else if (cardsMap.size() == 3) {
            if (cardsMap.containsValue(3)) return "三条";
            return "两对";
        } else if (cardsMap.size() == 4) {
            return "一对";
        } else {
            List<Map.Entry<Integer, Integer>> cardsList = new ArrayList<>(cardsMap.entrySet());
            if (suitsMap.size() == 1) {
                if (cardsList.get(cardsList.size() - 1).getKey() - cardsList.get(0).getKey() == 4) return "同花顺";
                else return "同花";
            }
            if (suitsMap.size() > 1 && cardsList.get(cardsList.size() - 1).getKey() - cardsList.get(0).getKey() == 4)
                return "顺子";
            return "无对";
        }
    }

    public static boolean isBetterJacks(List<Card> hand) {
        Map<Integer, Integer> cardsMap = new TreeMap<>();
        for (Card card : hand) {
            cardsMap.put(card.rank, cardsMap.getOrDefault(card.rank, 0) + 1);
        }
        for (Map.Entry<Integer, Integer> entry : cardsMap.entrySet()) {
            if (entry.getValue() > 1 && entry.getKey() >= 11){
                return true;
            }
        }
        return false;
    }

    public void shuffle() {
        Random random = new Random();
        for (int i = 0; i < cards.length; i++) {
            int randomIndex = random.nextInt(cards.length);
            Card tmp = cards[i];
            cards[i] = cards[randomIndex];
            cards[randomIndex] = tmp;
        }
    }

    public static int getScore(String getHandType, int score, int time,List<Card> hand) {
        if (getHandType.equals("一对")) {
            if(isBetterJacks(hand)){
                score += time;
            }
        } else if (getHandType.equals("两对")) {
            score += 5 * time;
        } else if (getHandType.equals("三条")) {
            score += 10 * time;
        } else if (getHandType.equals("顺子")) {
            score += 20 * time;
        } else if (getHandType.equals("同花")) {
            score += 40 * time;
        } else if (getHandType.equals("满堂红")) {
            score += 60 * time;
        } else if (getHandType.equals("四条")) {
            score += 80 * time;
        } else if (getHandType.equals("同花顺")) {
            score += 100 * time;
        } else {
            score -= time;
        }
        return score;
    }

    public static void main(String[] args) {
        Poker poker = new Poker();
        Scanner sc = new Scanner(System.in);
        int current_score = 100;
        poker.output();
        //请下注
        //1
        //current score 103
        //[方片J, 黑桃J, 方片A, 草花K, 草花Q]
        //一对
        //请输入你想换的牌[1-5],用空格分割,回车什么也不换
        //3 4 5
        //[方片J, 黑桃J, 黑桃A, 黑桃10, 黑桃Q]
        //一对
        while (true) {
            System.out.println("请下注");
            String pay_score = sc.nextLine();
            int score = Integer.parseInt(pay_score);
            current_score -= score;
            System.out.println("current score " + current_score);
            poker.shuffle();
            List<Card> hand = poker.getOneHand();
            System.out.println(hand);
            System.out.println(getHandType(hand));
            System.out.println("请输入你想换的牌[1-5],用空格分割,回车什么也不换");
            String str = sc.nextLine();
            Random random = new Random();
            if (!str.isEmpty()) {
                String[] split = str.split(" ");
                for (int i = 0; i < split.length; i++) {
                    int index = Integer.parseInt(split[i]);
                    int newIndex = random.nextInt(poker.cards.length - hand.size()) + hand.size();
                    hand.set(index - 1, poker.cards[newIndex]);
                }
            }
            System.out.println(hand);
            System.out.println(getHandType(hand));
            current_score = getScore(getHandType(hand), current_score, score,hand);
            System.out.println("current score " + current_score);
            if (current_score >= 200 || current_score <= 0) break;
        }

    }
}
